package cn.service.impl;

import cn.dao.UserDao;
import cn.dao.impl.UserDaoImpl;
import cn.entity.User;
import cn.service.UserService;
import cn.utils.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

    UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String username, String password) {
        User user = null;
        try {
            user = userDao.login(username);
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> findAllUser() {
        return userDao.findAllUser();
    }

    @Override
    public Long findUserCount() {
        Transaction tx = null;
        Long userCount = null;
        try {
            tx = HibernateUtil.getTransaction(HibernateUtil.currentSession());
            userCount = userDao.findUserCount();
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return userCount;
    }

    @Override
    public List<User> findPage(Integer pageNo, Integer pageSize) {
        Transaction tx = null;
        List<User> users = new ArrayList<User>();
        try {
            tx = HibernateUtil.getTransaction(HibernateUtil.currentSession());
            users = userDao.findPage(pageNo,pageSize);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return users;
    }

    @Override
    public List<User> findByUserName(String userName) {
        Transaction tx = null;
        List<User> users = new ArrayList<User>();
        try {
            tx = HibernateUtil.getTransaction(HibernateUtil.currentSession());
            users = userDao.findByUserName(userName);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return users;
    }

    @Override
    public Integer register(User user) {
        Integer id = 0;
        try {
            id = userDao.register(user);
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return id;
    }

    @Override
    public void saveUser(User user) {
        userDao.save(user);
    }

    @Override
    public void deleteUser(Integer id) {
        User userToDelete = userDao.get(id);
        userDao.delete(userToDelete);
    }

    @Override
    public void saveOrUpdateUser(User user) {
        userDao.saveOrUpdate(user);
    }

    @Override
    public void mergeUser(User user) {
        userDao.merge(user);
    }

    @Override
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    public User getUser(Integer id) {
        return userDao.get(id);
    }

    @Override
    public User loadUser(Integer id) {
        return userDao.load(id);
    }
}
